package com.ext.denacos.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

@Slf4j
@RestController
public class TestController {
    @Qualifier("redisTemplate")
    @Autowired
    private RedisTemplate template;


   /* @GetMapping("/get2")
    public Mono<Void> exchange(ServerWebExchange webExchange) {
        *//*ServerHttpResponse response = webExchange.getResponse();
        NettyDataBufferFactory factory = (NettyDataBufferFactory) response.bufferFactory();
        response.setStatusCode(HttpStatus.OK);
        response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
        DataBuffer dataBuffer = factory.allocateBuffer().write("Using exchange!".getBytes(StandardCharsets.UTF_8));
        return response.writeWith(Mono.just(dataBuffer));*//*
        *//* webExchange.getSession().flatMap(webSession -> {
             webSession.getAttributes().get(webSession.getId());
         });*//*
        return webExchange.getSession().flatMap(webSession -> {
                    String session = webSession.getAttributes().get(webSession.getId()).toString();
                    log.info("session+++++++++++:" + session);
                    return null;
                }
        );
    }*/

    @GetMapping(value = "/get5")
    public String get5(@RequestHeader("accessToken")  String accessToken) {
        if(accessToken !=null)
            log.info("success--------------"+accessToken);
        return "success";
    }


    @GetMapping(value = "/get3")
    public String cookie(@CookieValue("DOGE")  String DOGE) {
        return "success";
    }

    @GetMapping(value = "/get4")
    public String get4(HttpServletRequest request, HttpServletResponse response) {
        Cookie cookie = new Cookie("doge", "doge");
        cookie.setMaxAge(10); //设置cookie的过期时间是10s
        response.addCookie(cookie);
        return "success";
    }


    @GetMapping("/get1")
    public String getname(HttpServletRequest request) {
        String se = request.getSession().getId().toString();
        log.info(se + "------------------------");
        if (template.opsForValue().get(se) != null) {
            String session = template.opsForValue().get(se).toString();
            log.info("session=====:" + session);
        }
        return "success";
    }

    //整个gateway出现的一些问题1，jar包版本不对，springcloud 和springboot的版本不一致
    // 2，springboot和nacos的版本不一致  spring-cloud-starter-alibaba-nacos-discovery2.2.2 版本  spring-cloud-starter-gateway2.2.1版本没有问题，
    // 但是spring-cloud-starter-gateway若用2.2.2 就报错
    // 3，出现的nacos不能发现getway的问题 原因是yml的配置出错  discovery:  locator:  enabled: true  # 让gateway可以发现nacos中的微服务
    // 如果没有写上面的配置则不能发现gateway的配置 但是若是使用 uri: http://localhost:8762  这样的配置还是可以运行
    // 4，predicates:  - Path=/get/**   get在这里表示服务器的地址，所以filters: - StripPrefix=1 必须要写，否则不能发现
    // 5， - id: gatewayone  当多个服务的时候的id可以随便写，但是id不能重复，否则会错
}
